// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package dengues.system;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class ETLProcessClient implements Runnable {

    private java.net.Socket s;

    private java.io.PrintWriter pred;

    private Thread t;

    private boolean jobIsFinished = false;

    private String str = "";

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    public void run() {
        synchronized (this) {
            try {
                while (!jobIsFinished) {
                    wait(100);
                }
            } catch (InterruptedException e) {
                System.out.println("[trace] interrupted");
            }
        }
    }

    public void startThreadProcess(String clientHost, int portTraces) throws java.io.IOException, java.net.UnknownHostException {
        System.out.println("[process] connecting to socket on port " + portTraces);
        s = new java.net.Socket(clientHost, portTraces);
        pred = new java.io.PrintWriter(new java.io.BufferedWriter(new java.io.OutputStreamWriter(s.getOutputStream())), true);
        System.out.println("[process] connected");
        t = new Thread(this);
        t.start();

    }

    public boolean isPause() {
        java.io.InputStream in;
        try {
            askForStatus();
            in = s.getInputStream();
            java.io.LineNumberReader reader = new java.io.LineNumberReader(new java.io.InputStreamReader(in));
            return "PAUSE".equals(reader.readLine());
        } catch (java.io.IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    private void askForStatus() {
        pred.println("STATUS");
    }

    public void stopThreadProcess() {
        jobIsFinished = true;
        try {
            pred.close();
            s.close();
            System.out.println("[process] disconnected");
        } catch (java.io.IOException ie) {
        }
    }

    public void sendProcess(String componentId, String datas) {
        str = componentId + "->" + datas;
        pred.println(str); // envoi d'un message
    }

}
